package com.technology.education.dslitem

import android.widget.LinearLayout
import android.widget.TextView
import com.angcyo.dsladapter.DslAdapterItem
import com.angcyo.dsladapter.DslViewHolder
import com.technology.education.R
import com.technology.education.dslitem.style.DrawableStyle
import com.technology.education.ext.getColor
import com.technology.education.ext.getResId
import com.technology.education.widget.CommonButton
import me.hgj.jetpackmvvm.ext.util.dp2px

/**
 *@author:create by BZY
 *Date: 2020/8/4 17
 */
class ItemSingleButton(
    var title: String? = "",
    var tag: String? = "",
    var titleDrawable: Int = 0,
    var itemSpan: Int = -1
) : BaseStyleAdapterItem() {
    init {
        itemLayoutId = R.layout.item_single_button
        itemTag = tag
        itemSpanCount = itemSpan
    }

    override fun onItemBind(
        itemHolder: DslViewHolder,
        itemPosition: Int,
        adapterItem: DslAdapterItem, payload: List<Any>
    ) {
        super.onItemBind(itemHolder, itemPosition, adapterItem, payload)
        itemTag = tag
        style.titleTextColor = R.color.white
        style.titleTextSize = 18f
        style.leftPadding = 0
        style.rightPadding = 0
        itemHolder.v<CommonButton>(R.id.button)?.apply {
            text = title
            textSize = style.titleTextSize
            setTextColor(getColor(style.titleTextColor))
            background = getResId(if (titleDrawable > 0) titleDrawable else R.drawable.bg_button)
        }
        itemHolder.v<LinearLayout>(R.id.ll_parent)?.apply {
            setPadding(
                dp2px(style.leftPadding), dp2px(style.topPadding), dp2px(style.rightPadding),
                dp2px(style.bottomPadding)
            )
            if (style.itemBackColor > 0) {
                setBackgroundColor(getColor(style.itemBackColor))
            } else {
                setBackgroundColor(0)
                background =
                    if (style.itemBackDrawable > 0) getResId(style.itemBackDrawable) else null
            }
            gravity = style.parentGravity
        }
        itemHolder.click(R.id.button, _clickListener)
        if (style.itemHeight > 0) {
            itemHeight = style.itemHeight
        }
    }
}